﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace Test_2D
{
    [Serializable]
    public class Component
    {
        private string source;               //图片路径
        private Image fullImage;             //全图片
        private int actcount;                //动作数
        //private int fullWidth, fullHeight;   //全图片像素的宽高
        private int width, height;           //动作单元像素大小
        private string name;                 // = "3D动漫元测试"

        
        public Component(string _source, int _actcount, string _name)//int _fullWidth, int _fullHeight, 
        {
            source = _source;
            fullImage = Image.FromFile(_source);
            actcount = _actcount;
            //计算出动作单元像素大小!!!!!!实际中需要数据库给出！！！！！！！！！
            width = fullImage.Width / actcount;//fullWidth
            height = fullImage.Height;//fullHeight
            name = _name;
            //fullWidth = _fullWidth;
            //fullHeight = _fullHeight;

        }

        //不完善的构造方法
        public Component(string _source, int _actcount, int _fullWidth, int _fullHeight, int _width, int _height)
        {
            fullImage = Image.FromFile(_source);
            source = _source;
            actcount = _actcount;
            //fullWidth = _fullWidth;
            //fullHeight = _fullHeight;

            //width = _width;
            //height = _height;
        }

        /// <summary>
        /// 获取第_act个动作元素的位置矩形
        /// </summary>
        /// <param name="_act"></param>
        /// <returns></returns>
        public Rectangle getRec(int _act)
        {
            Rectangle rec = new Rectangle(new Point(width * (_act - 1), 0), new Size(width, height));
            return rec;
        }

        /// <summary>
        /// 获取第_act个动作元素的图片
        /// </summary>
        /// <param name="_act"></param>
        /// <returns></returns>
        public Image getImage(int _act) 
        {
            Bitmap bp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bp);
            g.DrawImage(fullImage, 0, 0, getRec(_act), GraphicsUnit.Pixel);
            return bp;
        }

        /// <summary>
        /// 获取元件的代表图片
        /// </summary>
        public Image Image
        {
            get { return getImage(1); }
        }

        /// <summary>
        /// 获取元件的全图片
        /// </summary>
        public Image FullImage
        {
            get { return fullImage; }
        }
        /// <summary>
        /// 元件大小
        /// </summary>
        public Size Size
        {
            get { return new Size(width, height); }
            set 
            { 
                Size size = (Size)value; 
                width = size.Width; 
                height = size.Height; 
            }
        }

        /// <summary>
        /// 元件宽度
        /// </summary>
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        /// <summary>
        /// 元件高度
        /// </summary>
        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        /// <summary>
        /// 获取元件全图片的大小
        /// </summary>
        public Size FullSize
        {
            get { return fullImage.Size; }
        }

        /// <summary>
        /// 获取元件名称
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// 获取元件动作数
        /// </summary>
        public int ActCount
        {
            get { return actcount; }
        }
    }
}
